package com.king01.controller;

import com.king01.entity.*;
import com.king01.service.OutlibraryService;
import com.king01.vo.AddOutLibraryVo;
import com.king01.vo.BranchVo;
import com.king01.vo.OutlibraryVo2;
import com.king01.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：lqs
 * @date ：Created in 2023/10/2 11:12
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/Oc")
public class OutlibraryController {
    @Autowired
    private OutlibraryService outlibraryService;
    @Autowired
    private HttpSession session;

    @RequestMapping("/select")
    public Result select(Integer currentPage, Integer pageSize){
        Result result = outlibraryService.selectAll(currentPage, pageSize);
        return result;
    }
    @RequestMapping("/lsadd")
    public Result add(@RequestBody OutlibraryVo2 outlibraryVo2,Integer outmethod){
        //先添加到表outlibrart数据
        Result result = outlibraryService.lsaddOutlibrary(outmethod);
        Result result1 = outlibraryService.updateBranch_goods(outlibraryVo2);
        return result1;


    }
    @RequestMapping("/pladd")
    public Result pladd(@RequestBody List<OutlibraryVo2> list, Integer outmethod){
        if (list.size()==0){
            return new Result(500,"数据为空",null);
        }
        for (OutlibraryVo2 vo2 : list) {
            if (vo2.getNum()==null){
                return new Result(500,"数据为空",null);
            }
        }
        //先添加到表outlibrart数据
        Result result = outlibraryService.addOutlibrary(outmethod);
        //获取表outlibrart最大的编号
        Integer result1 = (Integer)outlibraryService.maxId().getData();
        //
        for (OutlibraryVo2 vo : list) {
         vo.setTotal(vo.getSprice()*vo.getNum());
        };
        Result result2 = outlibraryService.addOutlibraryGoodspl(list, result1);
        return result2;
    }
    // 平调出库
    @RequestMapping("/pdadd")
    public Result pdadd(Integer newbranchid,@RequestBody List<AddOutLibraryVo> addOutLibraryVo){
        //添加数据
        Result result = outlibraryService.add2(newbranchid);
        //获取最新的cid
        Result result1 = outlibraryService.maxId2();
       //添加数据到outlibrarytransfer_goods中
        for (AddOutLibraryVo outLibraryVo : addOutLibraryVo) {
            Outlibrary_goods outlibrary_goods = new Outlibrary_goods();
            outlibrary_goods.setCid((Integer) result1.getData());
            outlibrary_goods.setGoodsid(outLibraryVo.getGoodsid());
            outlibrary_goods.setNum(outLibraryVo.getNum());
            outlibrary_goods.setTotal(outLibraryVo.getSprice()*outLibraryVo.getNum());
            Result result2 = outlibraryService.addByOg(outlibrary_goods);
        }
        return  result;
    }

    @RequestMapping("/success")
    public Result success(Integer cid){
        //根据cid查询出出库的商品
        List<Outlibrary_goods> outlibrary_goods = ( List<Outlibrary_goods>)outlibraryService.selectOutByCid(cid).getData();

        //获取当前店铺库存表的数据
        List<Branch_goods> branch_goods = (List<Branch_goods>)outlibraryService.selectBgByBranchid(null).getData();

        for (Outlibrary_goods good : outlibrary_goods) {
            for (Branch_goods branchGood : branch_goods) {
                if (good.getGoodsid()==branchGood.getGoodsid()) {
                   Integer aa = branchGood.getCounts()-good.getNum();
                   //判断是否大于库存
                   if (aa<0){
                       return new Result(500,"超出库存",null);
                   }
                   branchGood.setCounts(aa);
                }
            }
        };

        //修改通过后的数据
        for (Branch_goods goods : branch_goods) {
            Integer integer =(Integer)outlibraryService.updateSuccess(goods).getData();
            if (integer==0){
                return new Result(500,"插入失败",null);
            }
        }
        Integer updateoutlibrary = (Integer)outlibraryService.updateoutlibrary(cid).getData();
        return new Result(200,"通过成功",null);

    }
    @RequestMapping("/falseOh")
    public Result falseOh(Integer cid,String remarks){
        Result result = outlibraryService.updateoutlibrary2(cid, remarks);
        return result;
    }
    @RequestMapping("/falseOh2")
    public Result falseOh2(Integer cid,String remarks){
        Result result = outlibraryService.updateoutlibrary4(cid, remarks);
        return result;
    }
    @RequestMapping("/find")
    public Result find(Integer cid){
        Result result = outlibraryService.find(cid);
        return result;
    }
    @RequestMapping("/main")
    public Result main(Integer currentPage, Integer pageSize,@RequestBody OutlibraryVo2 outlibraryVo2){
        Result result = outlibraryService.selectAll2(currentPage,pageSize,outlibraryVo2);
        return  result;
    }
    //平调信息主表数据
    @RequestMapping("/main2")
    public Result main2(Integer currentPage, Integer pageSize,@RequestBody BranchVo branchVo){
        Result result = outlibraryService.selectMain(currentPage,pageSize,branchVo.getBranchid());
        return  result;
    }

    @RequestMapping("/trmain")
    public Result trmain(Integer currentPage,Integer pageSize){
        Result result = outlibraryService.selectAll3(currentPage,pageSize);
        return  result;
    }
    @RequestMapping("/find2")
    public Result find2(Integer cid){
        Result result = outlibraryService.find2(cid);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.READ_COMMITTED,
            readOnly = false //优化数据引擎。提高数据库效率。
    )
    @RequestMapping("/success2")
    public Result success2(Integer cid,Integer branchid){
        //根据cid查询出出库的商品
        List<Outlibrarytransfer_goods> outlibrarytransfer_goods = (List<Outlibrarytransfer_goods>)outlibraryService.selectOutByCid2(cid).getData();


        //获取对方店铺库存表的数据
        List<Branch_goods> branch_goods = (List<Branch_goods>)outlibraryService.selectBgByBranchid2(branchid).getData();



        for (Outlibrarytransfer_goods good : outlibrarytransfer_goods) {
            for (Branch_goods branchGood : branch_goods) {
                if (good.getGoodsid()==branchGood.getGoodsid()) {
                    Integer aa = branchGood.getCounts()-good.getNum();
                    //判断是否大于库存
                    if (aa<0){
                        return new Result(500,"超出库存",null);
                    }
                    branchGood.setCounts(aa);
                }
            }
        };
        //修改自己的数据
        Staff staff =(Staff)session.getAttribute("staff");
        List<Branch_goods> branch_goods2 = (List<Branch_goods>)outlibraryService.selectBgByBranchid(staff.getBranchid()).getData();



        for (Outlibrarytransfer_goods good : outlibrarytransfer_goods) {
            Boolean bb = true;
            for (Branch_goods branchGood : branch_goods2) {

                //如果存在这个商品则直接添加
                if (good.getGoodsid()==branchGood.getGoodsid()) {
                    Integer aa = branchGood.getCounts()+good.getNum();
                    branchGood.setCounts(aa);
                    bb=false;
                    Integer integer =(Integer)outlibraryService.updateSuccess(branchGood).getData();
                    if (integer==0){
                        return new Result(500,"插入失败",null);
                    }
                };
            }

        };

        for (Outlibrarytransfer_goods good : outlibrarytransfer_goods) {

            Boolean bb = true;
            for (Branch_goods branchGood : branch_goods2) {

                //如果存在这个商品则直接添加
                if (good.getGoodsid()==branchGood.getGoodsid()) {
                    bb=false;
                };
            }
            if (bb){
                //添加商品
                Result result = outlibraryService.addDoodsByBranchid(staff.getBranchid(), good.getGoodsid(),good.getNum());

            }

        };

















        //修改通过后的数据
        for (Branch_goods goods : branch_goods) {
            Integer integer =(Integer)outlibraryService.updateSuccess(goods).getData();
            if (integer==0){
                return new Result(500,"插入失败",null);
            }
        }


        Integer updateoutlibrary = (Integer)outlibraryService.updateoutlibrary3(cid).getData();
        return new Result(200,"通过成功",null);
    }
}

